#
#  Makefile for Horizon - kernel
#
#  Francesco Frigo
#  Tuesday 17th September, 2002
#
#  Updated on: Sunday 15th April, 2007
#

MAKE = make
CC = gcc-3.3
AS = as

CFLAGS = -nostdinc -nostdlib -nostartfiles -fno-builtin -fwritable-strings -Wall -W -g

INCLUDEDIR = -I./include -I./lib/include
LIBDIR = ./lib

MANAGERSDIR = ./managers
KICKSTARTDIR = ./kickstart

ENTRY = start
RELOC = 0x80000000
OUTF = system.bin
DBG_OUTF = system-dbg.o

MANAGERSOBJS =  \
		$(MANAGERSDIR)/vmm/*.o \
		$(MANAGERSDIR)/kbd/*.o \
		$(MANAGERSDIR)/tty/*.o \
		$(MANAGERSDIR)/hdd/*.o \
		$(MANAGERSDIR)/vfs/*.o \
		$(MANAGERSDIR)/dev/*.o \
		$(MANAGERSDIR)/bus/*.o

SRCS_CC := $(wildcard *.c)
SRCS_AS := $(wildcard *.s)

OBJS_CC := $(patsubst %.c, %.o, $(SRCS_CC))
OBJS_AS := $(patsubst %.s, %.o, $(SRCS_AS))

SRCS := $(SRCS_CC) $(SRCS_AS)
OBJS := $(OBJS_CC) $(OBJS_AS)

# Add other objects
KICKSTART_OBJS := $(KICKSTARTDIR)/*.o
EXTRA_OBJS :=  $(MANAGERSOBJS) $(LIBDIR)/libk.a $(LIBDIR)/libll.a

ALL_OBJS = $(KICKSTART_OBJS) $(OBJS) $(EXTRA_OBJS)

all: $(OUTF)

debug: $(OUTF)
	ld -T linker-dbg.ld -Ttext $(RELOC) --entry $(ENTRY) -L$(LIBDIR) -o $(DBG_OUTF) $(ALL_OBJS) --warn-common


$(OUTF): $(OBJS)
#	$(CC) -c $(CFLAGS) $(INCLUDEDIR) *.c
#	as -o i386_arch.o i386_arch.s
#	as -o interrupts.o interrupts.s
#	as -o spinlock.o spinlock.s
	$(MAKE) -C $(MANAGERSDIR)/vmm
	$(MAKE) -C $(MANAGERSDIR)/kbd
	$(MAKE) -C $(MANAGERSDIR)/tty
	$(MAKE) -C $(MANAGERSDIR)/hdd
	$(MAKE) -C $(MANAGERSDIR)/vfs
	$(MAKE) -C $(MANAGERSDIR)/dev
	$(MAKE) -C $(MANAGERSDIR)/bus
	$(MAKE) -C kickstart
	$(MAKE) -C lib
	ld -T linker.ld -Ttext $(RELOC) --entry $(ENTRY) -L$(LIBDIR) -o $(OUTF) $(ALL_OBJS) --warn-common

%.o: %.c
	$(CC) -c $(CFLAGS) $(INCLUDEDIR) -o $@ $<

%.o: %.s
	$(AS) -o $@ $<

clean: objremove
	$(MAKE) -C kickstart clean
	$(MAKE) -C lib clean
	$(MAKE) -C $(MANAGERSDIR)/vmm clean
	$(MAKE) -C $(MANAGERSDIR)/kbd clean
	$(MAKE) -C $(MANAGERSDIR)/tty clean
	$(MAKE) -C $(MANAGERSDIR)/hdd clean
	$(MAKE) -C $(MANAGERSDIR)/vfs clean
	$(MAKE) -C $(MANAGERSDIR)/dev clean
	$(MAKE) -C $(MANAGERSDIR)/bus clean
	rm -f *~ $(OUTF)

objremove:
	rm -f *.o

distclean:
	rm -f .depend
	$(MAKE) -C lib distclean
	$(MAKE) -C $(MANAGERSDIR)/vmm distclean
	$(MAKE) -C $(MANAGERSDIR)/kbd distclean
	$(MAKE) -C $(MANAGERSDIR)/tty distclean
	$(MAKE) -C $(MANAGERSDIR)/hdd distclean
	$(MAKE) -C $(MANAGERSDIR)/vfs distclean
	$(MAKE) -C $(MANAGERSDIR)/dev distclean
	$(MAKE) -C $(MANAGERSDIR)/bus distclean

depend .depend:
	$(CC) -MM $(CFLAGS) $(INCLUDEDIR) *.c 1>.depend
	$(MAKE) -C $(LIBDIR) depend
	$(MAKE) -C $(MANAGERSDIR)/vmm depend
	$(MAKE) -C $(MANAGERSDIR)/kbd depend
	$(MAKE) -C $(MANAGERSDIR)/tty depend
	$(MAKE) -C $(MANAGERSDIR)/hdd depend
	$(MAKE) -C $(MANAGERSDIR)/vfs depend
	$(MAKE) -C $(MANAGERSDIR)/dev depend
	$(MAKE) -C $(MANAGERSDIR)/bus depend
#	makedepend $(INCLUDEDIR) *.c >.depend

.PHONY: all clean objremove distclean depend

include .depend
